﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Conversion_Script
{
    public class CO2FixSimulations
    {
        static Matrix[] co2fix_simulationvalues;
        static Matrix landuse;
        static Random rnd = new Random();
        static int[] simlength;
        static int maxSimLength = int.MinValue;
        private static string[] variablelabels;
        static int varindexbiomc;
        private static ASCII co2year; // CO2Fix simulation year
      
        public static Matrix Landuse
        {
            get
            {
                return landuse;
            }
        }
        public static ASCII Co2year
        {
            get
            {
                return co2year;
            }
        }
        
        public static int[] SimLength
        {
            get
            {
                return simlength;
            }
        }
        public static int VarIndexBiomC
        {
            get
            {
                return varindexbiomc;
            }
        }
        
        public static Matrix[] CO2FIX_simulationvalues
        {
            get
            {
                return co2fix_simulationvalues;
            }

        }

        public static string[] VariableLabels
        {
            get
            {
                return variablelabels;
            }
        }
        public static void Initialize()
        {
            landuse = new Matrix(Settings.Get(KeyWords.LandUse));


            co2fix_simulationvalues = new Matrix[Landscape.LandUseNames.Length];



            for (int r = 0; r < Landscape.LandUseNames.Length; r++)
            {
                string fn = Landuse.GetValue(Landscape.LandUseNames[r], KeyWords.SimulationFileName);
                CO2FIX_simulationvalues[r] = new Matrix(fn);
                variablelabels = ArrayOperations.Remove(CO2FIX_simulationvalues[0].GetRow(0), 0);
                if (!ArrayOperations.IsIdentical(variablelabels, ArrayOperations.Remove(CO2FIX_simulationvalues[r].GetRow(0), 0)))
                {
                    throw new System.Exception("Outputvariables in simulation values are not identical");
                }

            }
            varindexbiomc = CO2FIX_simulationvalues[0].GetColumnNumber(KeyWords.BiomC);

            simlength = new int[Landscape.LandUseNames.Length];
            for (int land_use_index = 0; land_use_index < Landscape.LandUseNames.Length; land_use_index++)
            {
                string[] s = CO2FIX_simulationvalues[land_use_index].GetColumn(0);
                SimLength[land_use_index] = int.Parse(s[s.Length - 1]);
                if (SimLength[land_use_index] > maxSimLength) maxSimLength = SimLength[land_use_index];
            }
            // Ensure that for each land use code there is a simulation values

            for (int r = 0; r < Landscape.LandUseMap.Nrows; r++ )
            {
                for (int c = 0; c < Landscape.LandUseMap.Ncols; c++)
                {
                    int LandUse = Landscape.LandUseMap[r, c];
                    if (LandUse > CO2FIX_simulationvalues.Length)
                    {
                        throw new System.Exception("No CO2FixImplementation for mapcode " + LandUse);
                    }
                }
            }

            
        }
        
        public static void SetInitialCo2FixYear()
        {
            co2year = new ASCII(Landscape.LandUseMap );

            for (int r = 0; r < co2year.Nrows; r++)
            {
                for (int c = 0; c < co2year.Ncols; c++)
                {
                    try
                    {
                        co2year[r, c] = Landscape.GetInitialTime(r, c);
                    }
                    catch (System.Exception e)
                    {
                        int B = (int)Math.Round((double)Landscape.Outputvariables[VarIndexBiomC][r, c]);
                        co2year[r, c] = Landscape.GetInitialTime(r, c);  
                        throw new System.Exception("Error in InitializeCo2FixYear " + e.Message);
                    }

                }
            }

        }
   
         
        
        
         
       
        
     
         
    }
}
